package org.benxiong.iplay.util;

import java.text.DecimalFormat;

public class MeasurementUtil {
	public static Float poundsToKg(Float weightPounds) {
		return weightPounds * 0.453592F;
	}

	public static Float kgToPounds(Float weightPounds) {
		return weightPounds * 2.20462F;
	}

	public static Float inchToCm(Float heightInch) {
		return heightInch * 2.54F;
	}

	public static Float cmToInch(Float heightInch) {
		return heightInch * 0.393701F;
	}

	public static String inchToTxt(Float heightInch) {
		if (heightInch == null)
			return null;
		int feet = (int) (heightInch / 12);
		int inch = (int) (heightInch % 12);
		return feet + "'" + inch + "\"";
	}

	// miles
	public static double distanceMilesFrom(double lat1, double lng1, double lat2, double lng2) {
		double earthRadius = 3958.75;
		double dLat = Math.toRadians(lat2 - lat1);
		double dLng = Math.toRadians(lng2 - lng1);
		double sindLat = Math.sin(dLat / 2);
		double sindLng = Math.sin(dLng / 2);
		double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)
				* Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));
		double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
		double dist = earthRadius * c;

		return dist;
	}

	// meters
	public static double distanceMetersFrom(double lat1, double lng1, double lat2, double lng2) {
		return 1609.34 * MeasurementUtil.distanceMilesFrom(lat1, lng1, lat2, lng2);
	}

	public static String distanceMilesString(double lat1, double lng1, double lat2, double lng2) {
		// TODO: format 0000.00 miles
		double distance = distanceMilesFrom(lat1, lng1, lat2, lng2);
		DecimalFormat df = new DecimalFormat(".00");
		return df.format(distance) + " miles";
	}

	public static String distanceMeterssString(double lat1, double lng1, double lat2, double lng2) {
		// TODO: if less than 1000 meters, return 000 m

		// TODO: otherwise return 000.00 km
		double distance = distanceMetersFrom(lat1, lng1, lat2, lng2);
		if (distance > 1000) {
			DecimalFormat df = new DecimalFormat(".00");
			return df.format(distance / 1000) + "km";
		} else {
			return "" + ((int) distance) + " km";
		}
	}
}
